package com.spzx.order.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.order.service.IOrderInfoService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Arrays;

/**
 * @author Haoran Sun
 * @since 2024/08/02
 */

@Slf4j
@Component
public class OrderReceiver {

    @Resource
    private IOrderInfoService orderInfoService;

    @RabbitListener(queues = MqConst.QUEUE_CANCEL_ORDER)
    public void processCloseOrder(String orderId, Message msg, Channel channel) throws IOException {
        try {
            assert orderId != null;
            log.info("订单关闭：{}", orderId);
            orderInfoService.closeOrder(Long.parseLong(orderId));
            // 手动确认
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
        } catch (NumberFormatException e) {
            channel.basicNack(msg.getMessageProperties().getDeliveryTag(), false, true);
            throw new RuntimeException(e);
        }
    }

}
